package com.cyw.common.domain.common;

import com.cyw.common.constants.BaseStatusCode;
import com.cyw.common.constants.CommonConstant;
import com.cyw.common.constants.StatusCode;
import lombok.Data;
import lombok.experimental.Accessors;
import org.slf4j.MDC;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
@Accessors(chain = true)
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 510019869310111711L;

    private String code;
    private String msg;
    private String errorDetail;
    private T data;

    /**
     * traceId
     */
    private String tid;

    public Response() {

    }

    public Response(String code, String msg) {
        this.code = code;
        this.msg = msg;
        this.tid = MDC.get(CommonConstant.traceIdKey);
    }

    public Response(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.tid = MDC.get(CommonConstant.traceIdKey);
    }

    public Response(String code, String successMsg, T data, int count) {
        this.code = code;
        this.msg = successMsg;
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("total", count);
        dataMap.put("list", data);
        data = (T) dataMap;
        this.data = data;
        this.tid = MDC.get(CommonConstant.traceIdKey);

    }

    /**
     * 通用静态方法
     */
    public static Response success() {
        return new Response(BaseStatusCode.SUCCESS.getCode(), BaseStatusCode.SUCCESS.getUserTip(), null);
    }

    public static <T> Response<T> success(T data) {
        return new Response(BaseStatusCode.SUCCESS.getCode(), BaseStatusCode.SUCCESS.getUserTip(), data);
    }

    public static <T> Response<T> successWithId(Number data) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", data.toString());
        return new Response(BaseStatusCode.SUCCESS.getCode(), BaseStatusCode.SUCCESS.getUserTip(), map);
    }

    public static <T> Response<T> success(T data, int count) {
        return new Response(BaseStatusCode.SUCCESS.getCode(), BaseStatusCode.SUCCESS.getUserTip(), data, count);
    }

    public static <T> Response<T> success(String message, T data) {
        return new Response(BaseStatusCode.SUCCESS.getCode(), message, data);
    }

    public static Response fail() {
        return new Response(BaseStatusCode.ERROR_COMMON.getCode(), BaseStatusCode.ERROR_COMMON.getUserTip(), null);
    }

    public static <T> Response<T> fail(T data) {
        return new Response(BaseStatusCode.ERROR_COMMON.getCode(), BaseStatusCode.ERROR_COMMON.getUserTip(), data);
    }

    public static <T> Response<T> fail(String message) {
        return new Response(BaseStatusCode.ERROR_COMMON.getCode(), message);
    }

    public static <T> Response<T> fail(StatusCode code) {
        return new Response(code.getCode(), code.getUserTip());
    }

    public static <T> Response<T> fail(String code, String message, T data) {
        return new Response(code, message, data);
    }


}